package com.offcn.seckill;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableFeignClients
@EnableEurekaClient
@MapperScan(basePackages = {"com.offcn.seckill.dao"})
@EnableScheduling  // 定时任务的开启
@EnableAsync // 开启多线程
public class SeckillApp {
    public static void main(String[] args) {
        SpringApplication.run(SeckillApp.class);
    }

    // 环境变量对象
    @Autowired
    private Environment environment;
    // 队列
    @Bean
    public Queue createQueue(){
        return new Queue(environment.getProperty("mq.pay.queue.seckillorder"));
    }

    // 交换机
    @Bean
    public DirectExchange seckillExchange(){
        return new DirectExchange(environment.getProperty("mq.pay.exchange.seckillorder"));
    }

    // 队列和交换机的绑定
    @Bean
    public Binding seckillBinding(){
        return BindingBuilder.bind(createQueue()).to(seckillExchange()).with(environment.getProperty("mq.pay.routing.seckillkey"));
    }
}
